package com.dhcc.sdc.busi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sdc.busi.dao.SdcOperationStatisticsMapper;
import com.dhcc.sdc.busi.service.ISdcOperationStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("sdcOperationStatisticsService")
public class SdcOperationStatisticsServiceImpl implements ISdcOperationStatisticsService {

    @Autowired
    private SdcOperationStatisticsMapper sdcOperationStatisticsMapper;
    @Override
    public JSONObject getLogData(Map<String, Object> map) {
        Map<String, Object> homeData = sdcOperationStatisticsMapper.getLogSumData(map);
        JSONObject caseObj = new JSONObject();
        caseObj.put("todayCount", homeData.get("today_count"));
        caseObj.put("thisMonthCount", homeData.get("this_month_count"));
        caseObj.put("thisYearCount", homeData.get("this_year_count"));
        caseObj.put("logTotal", homeData.get("log_total"));
        //日志分类饼图
        JSONArray logTypeArray = new JSONArray();
        List<Map<String, Object>> logTypeList = sdcOperationStatisticsMapper.getLogTypeChartData(map);
        for (Map<String, Object> stringObjectMap : logTypeList) {
            logTypeArray.add(getJSONObj(2, "name", stringObjectMap.get("logType").toString(), "value", stringObjectMap.get("logCount").toString(),"",""));
        }
        caseObj.put("logTypeChartData", logTypeArray);
        //日志时间趋势图
        JSONArray logTimeArray = new JSONArray();
        JSONArray logTimeArray2 = new JSONArray();
        List<Map<String, Object>> logTimeList = sdcOperationStatisticsMapper.getLogTimeChartData(map);
        for (Map<String, Object> stringObjectMap : logTimeList) {
            logTimeArray.add(getJSONObj(2, "ddesc", stringObjectMap.get("logTime").toString(), "logCount", stringObjectMap.get("logCount").toString(), "", ""));
        }
        logTimeArray2.add(logTimeArray);
        caseObj.put("logTimeChartData", logTimeArray2);
        //病种统计柱图
        JSONArray qcEntityArray = new JSONArray();
        List<Map<String, Object>> qcEntityList = sdcOperationStatisticsMapper.getQCentityChartData(map);
        for (Map<String, Object> stringObjectMap : qcEntityList) {
            qcEntityArray.add(getJSONObj(2, "ddesc", CommonUtil.isEmpty(stringObjectMap.get("qcentityName"))?"":stringObjectMap.get("qcentityName").toString(), "logCount", stringObjectMap.get("logCount").toString(), "", ""));
        }
        caseObj.put("qcentityChartData", qcEntityArray);

        //科室统计柱图
        JSONArray departArray = new JSONArray();
        List<Map<String, Object>> departList = sdcOperationStatisticsMapper.getDepatChartData(map);
        for (Map<String, Object> stringObjectMap : departList) {
            departArray.add(getJSONObj(2, "ddesc",  CommonUtil.isEmpty(stringObjectMap.get("locDesc"))?"":stringObjectMap.get("locDesc").toString(), "logCount", stringObjectMap.get("logCount").toString(), "", ""));
        }
        caseObj.put("depatChartData", departArray);
        //用户统计柱图
        JSONArray userArray = new JSONArray();
        List<Map<String, Object>> userList = sdcOperationStatisticsMapper.getUserChartData(map);
        for (Map<String, Object> stringObjectMap : userList) {
            userArray.add(getJSONObj(2, "ddesc", (String)stringObjectMap.get("userName"), "logCount", stringObjectMap.get("logCount").toString(), "", ""));
        }
        caseObj.put("userChartData", userArray);
        return caseObj;
    }

    /**
     * @Description: 组装JSONObject
     * @return: JSONObject
     * @author: wujiafeng
     * @date:2020年1月3日 上午11:32:54
     */
    public JSONObject getJSONObj(int keyNum,String key1,String value1,String key2,String value2,String key3,String value3){
        JSONObject jsonObj = new JSONObject();
        if(keyNum==2){
            jsonObj.put(key1, value1);
            jsonObj.put(key2, value2);
        }else if(keyNum==3){
            jsonObj.put(key1, value1);
            jsonObj.put(key2, value2);
            jsonObj.put(key3, value3);
        }
        return jsonObj;
    }
}
